package com.example.ibo.lollipopdemo.Views;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Switch;
import android.widget.TextView;

import com.example.ibo.lollipopdemo.R;

/**
 * Created by Erqal on 16/1/19.
 */
public class SimpleItemView extends RelativeLayout {

    private int leftDrawable;
    private int leftDrawablePadding;
    private String leftText;
    private float leftTextSize;
    private int leftTextColor;
    private int rightDrawable;
    private int rightDrawablePadding;
    private String rightText;
    private float rightTextSize;
    private int rightTextColor;
    private String rightView;

    public SimpleItemView(Context context) {
        super(context);
        init();
    }

    public SimpleItemView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();

        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.simpleView);

        leftDrawable = a.getResourceId(R.styleable.simpleView_leftDrawable, R.drawable.ic_launcher);
        leftDrawablePadding = (int) a.getDimension(R.styleable.simpleView_leftDrawablePadding, 0);
        leftText = a.getString(R.styleable.simpleView_leftText);
        leftTextSize = a.getDimension(R.styleable.simpleView_leftTextSize, 24);
        leftTextColor = a.getColor(R.styleable.simpleView_leftTextColor, Color.BLACK);

        rightDrawable = a.getResourceId(R.styleable.simpleView_rightDrawable, R.drawable.ic_launcher);
        rightDrawablePadding = (int) a.getDimension(R.styleable.simpleView_rightDrawablePadding, 0);
        rightText = a.getString(R.styleable.simpleView_rightText);
        rightTextSize = a.getDimension(R.styleable.simpleView_rightTextSize, 24);
        rightTextColor = a.getColor(R.styleable.simpleView_rightTextColor, Color.BLACK);

        rightView = a.getString(R.styleable.simpleView_rightView);

        a.recycle();

        ImageView imageView = new ImageView(context);
        imageView.setId(1);
        imageView.setImageResource(leftDrawable);
        LayoutParams param1 = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
        param1.addRule(ALIGN_PARENT_LEFT);
        imageView.setPadding(leftDrawablePadding, leftDrawablePadding, leftDrawablePadding, leftDrawablePadding);
        addView(imageView, param1);

        TextView leftTextView = new TextView(context);
        leftTextView.setId(2);
        leftTextView.setText(leftText);
        leftTextView.setGravity(Gravity.CENTER_VERTICAL);
        leftTextView.setTextColor(leftTextColor);
        LayoutParams param2 = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
        param2.addRule(RIGHT_OF, 1);
        addView(leftTextView, param2);

        ImageView rightImageView = new ImageView(context);
        rightImageView.setId(4);
        rightImageView.setImageResource(rightDrawable);
        rightImageView.setPadding(rightDrawablePadding, rightDrawablePadding, rightDrawablePadding, rightDrawablePadding);
        LayoutParams param4 = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
        param4.addRule(ALIGN_PARENT_RIGHT);
        addView(rightImageView, param4);

        if (rightView != null && !rightView.equals("")) {
            View view = getViewForString(rightView);
            LayoutParams param3 = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
            param3.addRule(LEFT_OF, 4);
            addView(view, param3);
        } else {
            TextView rightTextView = new TextView(context);
            rightTextView.setText(rightText);
            rightTextView.setGravity(Gravity.CENTER_VERTICAL);
            rightTextView.setTextColor(rightTextColor);
            LayoutParams param3 = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
            param3.addRule(LEFT_OF, 4);
            addView(rightTextView, param3);
        }
    }

    private View getViewForString(String rightView) {
        switch (rightView) {
            case "switch":
                return new Switch(getContext());
            default:
                return new View(getContext());
        }
    }

    private void init() {

    }

    public int getLeftDrawable() {
        return leftDrawable;
    }

    public void setLeftDrawable(int leftDrawable) {
        this.leftDrawable = leftDrawable;
    }

    public String getLeftText() {
        return leftText;
    }

    public void setLeftText(String leftText) {
        this.leftText = leftText;
    }

    public float getLeftTextSize() {
        return leftTextSize;
    }

    public void setLeftTextSize(float leftTextSize) {
        this.leftTextSize = leftTextSize;
    }

    public int getLeftTextColor() {
        return leftTextColor;
    }

    public void setLeftTextColor(int leftTextColor) {
        this.leftTextColor = leftTextColor;
    }

    public int getRightDrawable() {
        return rightDrawable;
    }

    public void setRightDrawable(int rightDrawable) {
        this.rightDrawable = rightDrawable;
    }

    public String getRightText() {
        return rightText;
    }

    public void setRightText(String rightText) {
        this.rightText = rightText;
    }

    public float getRightTextSize() {
        return rightTextSize;
    }

    public void setRightTextSize(float rightTextSize) {
        this.rightTextSize = rightTextSize;
    }

    public int getRightTextColor() {
        return rightTextColor;
    }

    public void setRightTextColor(int rightTextColor) {
        this.rightTextColor = rightTextColor;
    }

    public String getLeftView() {
        return rightView;
    }

    public void setLeftView(String rightView) {
        this.rightView = rightView;
    }

    public float getLeftDrawablePadding() {
        return leftDrawablePadding;
    }

    public void setLeftDrawablePadding(int leftDrawablePadding) {
        this.leftDrawablePadding = leftDrawablePadding;
    }

    public float getRightDrawablePadding() {
        return rightDrawablePadding;
    }

    public void setRightDrawablePadding(int rightDrawablePadding) {
        this.rightDrawablePadding = rightDrawablePadding;
    }
}
